<?php

class M_books extends CI_Model {
	
	private $table_name = "zy_books";

    public function __construct()
    {
        parent::__construct();
		$this->load->database();
    }
    
    /**
     * 检索出图书列表
	 * 
	 * @param $page 商品列表的起始记录
	 * @param $pageSize 每页展示的商品数量
	 * @param $order_type 商品排序的规则
	 * 
     * @return array
     */
    public function selectBooks( $page , $pageSize , $order_type='' , $where = '' , $index = FALSE)
    {
    	$order = '';
    	if($order_type)
		{
			if($order_type=='price')
			{
				$order = "ORDER BY `book_price` DESC,`book_store` DESC";
			}
			else if($order_type=='hot')
			{
				$order = "ORDER BY `book_click` DESC,`book_store` DESC";
			}
		}
		else
		{
		
			$order = "ORDER BY `book_store` DESC";
			
		}
		
		if($index)
		{
			$query = "SELECT * FROM `{$this->table_name}` AS b WHERE `book_status`='1' {$where} ORDER BY `submit_time` DESC LIMIT {$page},{$pageSize}";
		}
		else 
		{
			$query = "SELECT * FROM `{$this->table_name}` AS b WHERE `book_status`='1' {$where} {$order} LIMIT {$page},{$pageSize}";
		}
		
    	

		return $this->db->query($query)->result();
    }
    
    /**
     * 检索出书本的所有数量
     *
     * @return array 数量
     */
    public function selectbooksAmount($where = '')
    {
    	$query = "SELECT COUNT(*) AS bookAmount FROM `{$this->table_name}` AS b WHERE `book_status`='1' {$where}";

		return $this->db->query($query)->result();
    }
    
    /**
     * 根据图书名称查找图书信息
     * 
     * @param string $book_name 图书的名称
     * 
     * @return void
     */
    public function selectBookByNameOnly($book_name)
    {
    	return $this->db->get_where($this->table_name,array('book_name'=>$book_name),'1','0')->result();
    }
    
    /**
     * 根据图书ISBN查找图书信息
     *
     * @param string $isbn 图书的ISBN
     *
     * @return void
     */
    public function selectBookByIsbnOnly($isbn)
    {
    	return $this->db->get_where($this->table_name,array('book_isbn'=>$isbn),'1','0')->result();
    }
    
    /**
     * 根据书本ID获取书本信息
     *
     * @param int $book_id 书本ID
     *
     * @return array 书本信息组成的数组
     */
    public function selectBookById($book_id)
    {
    	$query = "SELECT b.*,c.`cate_type`,p.`pub_id`,p.`pub_name` FROM `zy_books` AS b LEFT JOIN `zy_publish` AS p ON b.`book_publish`=p.`pub_id`
    	LEFT JOIN `zy_category` as c ON b.`book_category`=c.`cate_id` WHERE b.`book_id`='{$book_id}' LIMIT 0,1";
		return $this->db->query($query)->result();
    }
    
    /**
     * 根据书本ID获取书本库存数量
     * 
     * @param int $book_id 书本的ID
     * 
     * @return 书本库存数量组成的变量数组
     */
    public function selectBookAmountById($book_id)
    {
    	$this->db->select("book_store,book_price,book_name");
    	return $this->db->get_where($this->table_name , array("book_id"=>$book_id))->result();
    }
	
	/**
	 * 购买完成之后减少库存总量
	 */
	public function reduceStoreAmount($amount,$book_id)
	{
		$query = "UPDATE `{$this->table_name}` SET book_store=book_store-".$amount." , book_buy=book_buy+".$amount." WHERE `book_id`='".$book_id."'";
		if($this->db->query($query))
		{
			return true;
		}
	}

    /**
     * 更新书本访问数目
	 *
	*/
    public function reduceBookView($book_id)
    {
        $query = "UPDATE `{$this->table_name}` SET book_click=book_click+1 WHERE `book_id`='".$book_id."'";
        if($this->db->query($query))
        {
            return true;
        }
    }

    /**
	 * 检索书本列表，用于搜索功能
	 */
	public function searchBooksByAll($condition ,$page , $pagesize , $is_cate =FALSE)
    {
    	if(!$is_cate)
    	{
    		$query = "SELECT * FROM `zy_books` WHERE `book_name` LIKE '%{$condition}%' OR `book_author` LIKE '%{$condition}%' LIMIT {$page},{$pagesize}";
    	}
    	else
    	{
    		$query = "SELECT * FROM `zy_books` as b LEFT JOIN `zy_category` as c 
    		ON b.`book_category`=c.`cate_id`
    		WHERE b.`book_name` LIKE '%{$condition}%' 
    		OR b.`book_author` LIKE '%{$condition}%'
    		OR c.`cate_name` LIKE '%{$condition}%'
    		LIMIT {$page},{$pagesize}";
    	}
    	return $this->db->query($query)->result();
    }
    
	/**
	 * 检索书本列表，用于搜索功能,查找数量
	 */
	public function searchBooksAmountByAll($condition , $is_cate)
    {
    	if(!$is_cate)
    	{
    		$query = "SELECT COUNT(*) AS bookAmount FROM `zy_books` WHERE `book_name` LIKE '%{$condition}%' OR `book_author` LIKE '%{$condition}%' ORDER BY `book_store` DESC";
    	}
    	else
    	{
    		$query = "SELECT COUNT(*) AS bookAmount FROM `zy_books` as b LEFT JOIN `zy_category` as c 
    		ON b.`book_category`=c.`cate_id`
    		WHERE b.`book_name` LIKE '%{$condition}%' 
    		OR b.`book_author` LIKE '%{$condition}%'
    		OR c.`cate_name` LIKE '%{$condition}%' ORDER BY `book_store` DESC";
    	}
    	return $this->db->query($query)->result();
    }
    
    /**
     * 通过条件匹配书本名称和isbn号，模糊匹配
     */
    public function searchByNameIsbn($search , $page , $pagesize)
    {
    	$this->db->order_by('book_store','desc');
    	$this->db->like('book_name',$search);
    	$this->db->or_like('book_isbn',$search);
    	
    	return $this->db->get($this->table_name ,$pagesize , $page)->result();
    }
    
	/**
     * 通过条件匹配书本名称和isbn号，模糊匹配，查找数量
     */
    public function searchAmountByNameIsbn($search)
    {
    	$this->db->like('book_name',$search);
    	$this->db->or_like('book_isbn',$search);
    	
    	$this->db->from($this->table_name);
		return $this->db->count_all_results();
    }
    
    /**
     * 根据书本的ISBN号查询书本记录
     * 
     * @param int $isbn 书本的ISBN号
     * 
     * @return int insert_id 用户的ID
     */
    public function searchBookByIsbn($isbn, $page , $pagesize)
    {
    	$this->db->order_by('book_store','desc');
    	return $this->db->get_where($this->table_name,array('book_isbn'=>$isbn) , $pagesize , $page)->result();
    }
    
	/**
     * 根据书本的ISBN号查询记录条数
     * 
     * @param int $isbn 书本的ISBN号
     * 
     * @return int insert_id 用户的ID
     */
    public function searchBookAmountByIsbn($isbn)
    {
    	$query = "SELECT COUNT(*) AS bookAmount FROM `{$this->table_name}` WHERE `book_isbn`='{$isbn}'";
    	return $this->db->query($query)->result();
    }
    
    /**
     * 缺货登记
     */
    public function bookRegistration($book_id , $user_phone , $book_amount , $most_days)
    {
    	$data = array(
    		'reg_user_phone'=>$user_phone,
    	'book_id'=>$book_id,
    	'book_amount'=>$book_amount,
    	'reg_time'=>time(),
    	'reg_most_days'=>$most_days
    	);
    	if($this->db->insert('zy_registration',$data))
    	{
    		return true;
    	}
    }
	

	
	
}